﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using BitAuto.EP.CRMLogSystem.Entity;
using CRMSimple.LogService.DataProtocol.Abstractions;
using BitAuto.EP.CRMLogSystem.Utils;
using System.Data;
using CRMSimple.LogService.DataConsumer.Abstractions;
using Newtonsoft.Json.Linq;
using CRMSimple.LogService.DataCacheExtensions.FileDataCache;

namespace CRMSimple.LogServices
{
    public static class TextFileDataCacheExtensions
    {
       #region 由缓存定义的数据处理方法
		 /// <summary>
        /// 反序列化数据
        /// </summary>
        public static IDictionary<string, Tuple<string, IList<DataParam>>> DeserializeDataParam(this DataParam data)
        {
            return JsonHelper.Deserialize<IDictionary<string, Tuple<string, IList<DataParam>>>>(data.Data);
        }
        /// <summary>
        /// 填充数据
        /// </summary>
        public static void FillData(this LogTableInfo info,Tuple<string, IList<DataParam>> list)
        {
            info.Table.Clear();
            info.FilePath = list.Item1;
            foreach (var item in list.Item2)
            {
                info.FillRow(item);
            }
        }
        /// <summary>
        /// 填充一行数据
        /// </summary>
        public  static void FillRow(this LogTableInfo info, DataParam data)
        {
            DataRow row = info.Table.NewRow();
            ProduceDataForMQ proInfo = new ProduceDataForMQ();
            proInfo.ConvertToModule(data.Data);
            JObject jobj = JsonHelper.DeserializeObject(proInfo.LogContent);
            foreach (DataColumn Column in info.Table.Columns)
            {
                if (Column.ColumnName.Equals("logContent"))
                    row[Column.ColumnName] = jobj[Column.ColumnName].ToString();//+"_" + Path.GetFileName(info.FilePath);
                else
                    row[Column.ColumnName] = jobj[Column.ColumnName].ToString();
            }
            info.Table.Rows.Add(row);
        }
          /// <summary>
        /// 添加异常文件路径
        /// </summary>
        public static void AddExceptionFile(this LogTableInfo info,ConfigurationManager config)
        {
            var ErrorFiles = config[info.LogTableId, "ErrorFiles"];
            //将异常文件标为异常
            File.SetAttributes(info.FilePath,FileAttributes.Device);

            if (string.IsNullOrEmpty(ErrorFiles))
                config[info.LogTableId, "ErrorFiles"] = string.Format("[\"{0}\"]",Path.GetFileName(info.FilePath));
            else
            {
                JArray arry = JArray.Parse(ErrorFiles);
                arry.Add(Path.GetFileName(info.FilePath));
                //config[info.LogTableId, "LastErrorFileName", "wait"] = Path.GetFileName(info.FilePath);
                config[info.LogTableId, "ErrorFiles"] = arry.ToString();
            }
        }
        /// <summary>
        /// 获取实体类型
        /// </summary>
        public static T GetModule<T>(this FileDataCacheRoot fileConfigurationRoot, string json) where T : class
        {
            return JsonHelper.Deserialize<T>(json);
        }
        ///// <summary>
        ///// 填充数据
        ///// </summary>
        //public static string RestoreDataTable(this LogTableInfo info)
        //{

        //    foreach (var item in info.Table.Rows)
        //    {
                
        //    }
        //}
        //public static string RestoreDataRow(this LogTableInfo info,DataRow row)
        //{
        //    JProperty[] pros=new JProperty[info.Table.Columns.Count];
        //    int count=0;
        //    foreach (DataColumn Column in info.Table.Columns)
        //    {
        //        pros[count++] =new JProperty( Column.ColumnName,row[Column.ColumnName]);
        //    }
        //    return new JObject(pros).ToString();
        //}
       #endregion
        public static string GetPath(this TextFileDataCache textFileDataCache,string key,string fileName)
        {
            return Path.Combine(textFileDataCache[key], fileName);
        }
    }
}
